// --------------------
// -- TEST FUNCTIONS --
// --------------------
if(!tests.scenarios) {
	tests.scenarios = {};
}
tests.scenarios.Gossip = {name: "Gossip"};

tests.scenarios.Gossip.StateUpdateIsDelegated = function(){
    // given
    var oldTheory = mindgraph.theory;
    mindgraph.theory = createTheory();
    
    // when
    mindgraph.gossip.stateUpdate();
    
    // then
    var resultObject = new tests.prv.ResultObject();
    var assert = resultObject.createAssert();

    assert(wave.getStateCount === 1);
    assert(mindgraph.theory.stateHandlerCount === 1);
    resultObject.resultMessage = "StateCount: " + wave.getStateCount + ", stateHandlerCount: " + mindgraph.theory.stateHandlerCount;
    
    mindgraph.theory = oldTheory;
    return resultObject;
}

tests.scenarios.Gossip.InitSetsStateHandler = function(){
    // given
    wave.setStateCallbackCount = 0;
    wave.isInWaveContainerCount = 0;
    
    // when
    mindgraph.gossip.init();
    
    // then
    var resultObject = new tests.prv.ResultObject();
    var assert = resultObject.createAssert();

    assert(wave.setStateCallbackCount === 1);
    assert(wave.isInWaveContainerCount === 1);
    resultObject.resultMessage = "setStateCallbackCount: " + wave.setStateCallbackCount;
    resultObject.resultMessage += ", isInWaveContainerCount: " + wave.isInWaveContainerCount;
    return resultObject;
}

tests.scenarios.Gossip.InitCallsOrigamiInit = function(){
    // given
    var oldInit = mindgraph.origami.init;
    var callCount = 0;
    var callArgs = undefined;
    mindgraph.origami.init = function () {
        callCount++;
        callArgs = arguments;
    }

    // when
    mindgraph.gossip.init();

    // then
    var resultObject = new tests.prv.ResultObject();
    var assert = resultObject.createAssert();

    assert(callCount === 1);
    assert(callArgs !== undefined);
    assert(callArgs.length === 0);
    resultObject.setResultMessage(callArgs);
    mindgraph.origami.init = oldInit;
    return resultObject;
}

tests.scenarios.Gossip.StateUpdateCanHandleJSON = function () {
    // given
    var k1 = "key1";
    var k2 = "key2";
    var state = createState();
    state.setKeys([k1, k2]);
    var oldTheory = mindgraph.theory;
    mindgraph.theory = createTheory();
    wave.state = state;

    // when
    mindgraph.gossip.stateUpdate();
    
    // then
    var resultObject = new tests.prv.ResultObject();
    var assert = resultObject.createAssert();

    var stp = mindgraph.theory.statePassed;
    assert(stp !== undefined);
    assert(stp[k1] !== undefined);
    assert(stp[k2] !== undefined);
    assert(typeof stp[k1] === "object");
    assert(typeof stp[k2] === "object");
    assert(stp[k1].id === k1);
    assert(stp[k2].id === k2);
    assert(stp[k1].text === k1 + " text");
    assert(stp[k2].text === k2 + " text");
    resultObject.setResultMessage(stp);
    mindgraph.theory = oldTheory;

    return resultObject;
}

tests.scenarios.Gossip.DeltaIsSentToWave = function () {
    // given
    wave.getStateCount = 0;
    wave.state.submitDeltaCount = 0;
    wave.state.delta = undefined;
    var testObject = {
        id: "testUser123",
        text: "Some Text"
    };

    // when
    mindgraph.gossip.sendDelta(testObject);

    // then
    var resultObject = new tests.prv.ResultObject();
    var assert = resultObject.createAssert();

    assert(wave.getStateCount === 1);
    assert(wave.state.submitDeltaCount === 1);
    assert(wave.state.delta !== undefined);
    assert(wave.state.delta[testObject.id] === JSON.stringify(testObject));

    resultObject.setResultMessage(wave.state.delta[testObject.id].replace(/"/g, "&quot;"));
    return resultObject;
}
// ---------------------------
// -- END of TEST FUNCTIONS --
// ---------------------------
